package com.liangsui.mss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.User;
import com.liangsui.mss.dto.UserDto;
import com.liangsui.mss.mapper.RoleMapper;
import com.liangsui.mss.mapper.UserMapper;
import com.liangsui.mss.service.UserService;
import com.liangsui.mss.utils.AppMd5Utils;
import com.liangsui.mss.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author zhang
* @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
* @createDate 2022-12-03 16:14:27
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public User queryUserByPhone(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq(User.COL_PHONE, phone);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    /**
     * 根据用户ID查询用户
     *
     * @param userId 用户编号
     * @return
     */
    @Override
    public User getUserById(Long userId) {
        return this.userMapper.selectById(userId);
    }

    /**
     * 分页查询用户
     *
     * @param userDto
     * @return
     */
    @Override
    public DataGridView listUserForPage(UserDto userDto) {
        Page<User> page = new Page<>(userDto.getPageNum(), userDto.getPageSize());
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(userDto.getUserName()),
                User.COL_USER_NAME, userDto.getUserName());
        qw.like(StringUtils.isNotBlank(userDto.getPhone()),
                User.COL_PHONE, userDto.getPhone());
        qw.eq(StringUtils.isNotBlank(userDto.getStatus()),
                User.COL_STATUS, userDto.getStatus());
        qw.eq(userDto.getDeptId() != null,
                User.COL_DEPT_ID, userDto.getDeptId());
        /* 查询时间段 time>xxx and xxx<time */
        qw.ge(null != userDto.getBeginTime(),
                User.COL_CREATE_TIME, userDto.getBeginTime());
        qw.le(null != userDto.getEndTime(),
                User.COL_CREATE_TIME, userDto.getEndTime());
        qw.orderByAsc(User.COL_USER_ID);
        this.userMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());

    }

    /**
     * 添加用户
     *
     * @param userDto
     * @return
     */
    @Override
    public int addUser(UserDto userDto) {
        User user = new User();
        BeanUtil.copyProperties(userDto, user);
        user.setUserType(Constants.USER_NORMAL);
        user.setCreateBy(userDto.getSimpleUser().getUserName());
        user.setCreateTime(DateUtil.date());
        user.setSalt(AppMd5Utils.createSalt());
        String defaultPwd = user.getPhone().substring(5);
        user.setPassword(AppMd5Utils.md5(defaultPwd, user.getSalt(), 2));
        return this.userMapper.insert(user);
    }

    /**
     * 修改用户
     *
     * @param userDto
     * @return
     * @保证数据唯一
     */
    @Override
    public int updateUser(UserDto userDto) {
        // 查询出唯一数据
        User user = this.userMapper.selectById(userDto.getUserId());
        if (null == user) {
            return 0;
        }
        // 浅克隆
        BeanUtil.copyProperties(userDto, user);
        user.setUpdateBy(userDto.getSimpleUser().getUserName());
        return this.userMapper.updateById(user);
    }

    /**
     * 删除用户
     *
     * @param userIds
     * @return
     */
    @Override
    public int deleteUserByIds(Long[] userIds) {
        List<Long> ids = Arrays.asList(userIds);
        // 删除用户需要删除与之关联的表数据sys_role_user 角色
        if (ids != null && ids.size() > 0) {
            this.roleMapper.deleteRoleUserByUserIds(ids);
            return this.userMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    /**
     * 查询所有可用的用户
     *
     * @return
     */
    @Override
    public List<User> getAllUsers() {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq(User.COL_STATUS, Constants.STATUS_TRUE);
        qw.eq(User.COL_USER_TYPE, Constants.USER_NORMAL);
        qw.orderByAsc(User.COL_USER_ID);
        return this.userMapper.selectList(qw);
    }

    /**
     * 重置用户密码
     *
     * @param userIds
     */
    @Override
    public void resetPassWord(Long[] userIds) {
        for (Long userId : userIds) {
            User user = this.userMapper.selectById(userId);
            String defaultPwd = "";
            if (user.getUserType().equals(Constants.USER_ADMIN)) {
                defaultPwd = "zls123";
            } else {
                defaultPwd = user.getPhone().substring(5);
            }
            user.setSalt(AppMd5Utils.createSalt());
            user.setPassword(AppMd5Utils.md5(defaultPwd, user.getSalt(), 2));
            this.userMapper.updateById(user);
        }
    }

    @Override
    public List<User> queryUsersNeedScheduling(Long userId, Long deptId) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq(null != deptId, User.COL_DEPT_ID,deptId);
        wrapper.eq(null != userId, User.COL_USER_ID,userId);
        wrapper.eq(User.COL_SCHEDULING_FLAG, Constants.SCHEDULING_FLAG_TRUE);//需要排班的
        wrapper.eq(User.COL_STATUS, Constants.STATUS_TRUE);//可用的用户
        return this.userMapper.selectList(wrapper);
    }
}




